'use strict';

const path = require('path');

function resolve(dir) {
  return path.join(__dirname, dir);
}

module.exports = {
  publicPath: './',
  outputDir: 'dist',
  assetsDir: 'static',
  productionSourceMap: false,
  devServer: {
    proxy: {
      '/api': {
        target: 'http://192.168.1.136:4010', // 本地接口
        // target: 'http://192.168.1.136:4010', // 线上接口
        changeOrigin: true
      }
    },
  },
  configureWebpack: {
    resolve: {
      alias: {
        '@': resolve('src'),
        '@a': resolve('src/assets'),
        '@c': resolve('src/components'),
        '@v': resolve('src/views'),
        '@u': resolve('src/utils'),
        '@m': resolve('src/mixins')
      }
    },
  },
  transpileDependencies: [ // 指定对第三方组件也进行babel-polyfill处理
    'v-contextmenu',
    'vue-runtime-helpers',
  ],
  chainWebpack(config) {
    config.plugins.delete('preload'); // 关闭预加载
    config.plugins.delete('prefetch'); // 关闭预读取

    config.module.rule('vue').use('vue-loader').loader('vue-loader').tap(options => { // vue 去掉元素之间空格 preserveWhitespace
      options.compilerOptions.preserveWhitespace = true;
      return options;
    }).end();

    config.when(process.env.NODE_ENV !== 'development', config => {
      config.plugin('ScriptExtHtmlWebpackPlugin').after('html').use('script-ext-html-webpack-plugin', [{
        // "runtime" 必须与runtimeChunk名称相同，默认是 "runtime"
        inline: /runtime\..*\.js$/
      }]).end();
      config.optimization.splitChunks({
        chunks: 'all',
        cacheGroups: {
          libs: {
            name: 'chunk-libs',
            test: /[\\/]node_modules[\\/]/,
            priority: 10,
            chunks: 'initial' // 只打包初始时依赖的第三方
          },
          elementUI: {
            name: 'chunk-elementUI', // 单独将 elementUI 拆包
            priority: 20, // 权重要大于 libs 和 app 不然会被打包进 libs 或者 app
            test: /[\\/]node_modules[\\/]_?element-ui(.*)/ // in order to adapt to cnpm
          },
          commons: {
            name: 'chunk-commons',
            test: resolve('src/components'), // 可自定义拓展你的规则
            minChunks: 3, // 最小共用次数
            priority: 5,
            reuseExistingChunk: true
          }
        }
      });
      config.optimization.runtimeChunk('single');
    });
  },
  css: {
    loaderOptions: {
      sass: {
        prependData: '@import "./src/components/Layout/styles/theme.scss";',
      }
    }
  }
};
